package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.imp.CourierStationDaoImpl;
import com.hs.panghu.entity.CourierStation;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/courierStation")
public class CourierStationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        //连接数据库
        CourierStationDaoImpl courierStationDao = new CourierStationDaoImpl();
//        获取驿站
        if("courierStationList".equals(action)){
            Map<String,String> map = new HashMap<>();
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String courierStationName = req.getParameter("courierStationName");
            String communityName = req.getParameter("courierStationCommunityName");
            String courierStationStatus = req.getParameter("courierStationStatus");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("courierStationName",courierStationName);
            map.put("communityName",communityName);
            map.put("courierStationStatus",courierStationStatus);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            //获取集合
            List<CourierStation> list = courierStationDao.selectCourierStations(map);
            //获取总条数
            int count = courierStationDao.selectCourierStationCount(map);
            //创建一个返回值的map集合用来包裹list和count总数
            Map resultMap = new HashMap();
            resultMap.put("list",list);
            resultMap.put("count",count);
            //把结果封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));

        }
//        添加驿站
        if("addCourierStation".equals(action)){
            String courierStationAddress = req.getParameter("courierStationAddress");
            Integer courierStationCommunityId = Integer.valueOf(req.getParameter("courierStationCommunityId"));
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            CourierStation courierStation = new CourierStation(
                    courierStationAddress,
                    courierStationCommunityId,
                    courierStationCommunityName,
                    courierStationImage,
                    courierStationName
            );

            int row = courierStationDao.insertCourierStation(courierStation);
            if(row>0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else{
                Result success = Result.error();
                resp.getWriter().write(JSON.toJSONString(success));
            }
        }
//        修改驿站
        if("updateCourierStation".equals(action)){
            String courierStationAddress = req.getParameter("courierStationAddress");
            Integer courierStationCommunityId = Integer.valueOf(req.getParameter("courierStationCommunityId"));
            Integer courierStationId = Integer.valueOf(req.getParameter("courierStationId"));
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            CourierStation courierStation = new CourierStation(
                    courierStationAddress,
                    courierStationCommunityId,
                    courierStationId,
                    courierStationImage,
                    courierStationName
            );
            int row = courierStationDao.updateCourierStation(courierStation);
            if(row>0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else{
                Result success = Result.error();
                resp.getWriter().write(JSON.toJSONString(success));
            }
        }
//        上下架
        if("upOrDown".equals(action)){
            Integer courierStationId = Integer.valueOf(req.getParameter("courierStationId"));
            Integer courierStationStatus = Integer.valueOf(req.getParameter("courierStationStatus"));
            CourierStation courierStation = new CourierStation(
                    courierStationId,
                    courierStationStatus
            );
            int row = courierStationDao.upOrDownCourierStation(courierStation);
            if(row>0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else{
                Result success = Result.error();
                resp.getWriter().write(JSON.toJSONString(success));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
